package com.example.dao;

import com.example.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserDao {
    /*CREATE TABLE `users` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL,
  `role_id` int NOT NULL,
  `status` tinyint DEFAULT '1',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  KEY `role_id` (`role_id`),
  CONSTRAINT `users_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
) */


    @Select("SELECT * FROM users WHERE id = #{id}")
    User getUserById(int id);

    @Select("SELECT t1.*, " +
            "t2.id AS 'role.id', " +
            "t2.name AS 'role.name', " +
            "t2.description AS 'role.description' "+
            "FROM users t1 INNER JOIN roles t2 ON t1.`role_id`=t2.`id`")
    List<User> getUsers();

    @Insert("INSERT INTO users (username, password, role_id, status) VALUES (#{username}, #{password}, #{roleId}, #{status})")
    void saveUser(User user);

    @Update("UPDATE users SET username = #{username}, password = #{password}, role_id = #{roleId}, status = #{status} WHERE id = #{id}")
    void updateUser(User user);

    @Delete("DELETE FROM users WHERE id = #{id}")
    void deleteUser(int id);
}
